package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.BArticleTableInfo;
import com.tulele.vtool.model.combinedEntity.CombinedAritcleTableDetail;
import com.tulele.vtool.model.combinedEntity.CombinedAritcleTableInfo;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.ArticleTableInfoService;
import com.tulele.vtool.service.IArticleInfoService;
import com.tulele.vtool.task.async.ArticleTableInfoTask;
import com.tulele.vtool.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 用户文章信息采集表单
 */
@RestController
@Slf4j
public class ArticleTableInfoHandler {
    @Autowired
    private ArticleTableInfoService articleTableInfoService;

    @Autowired
    private IArticleInfoService articleInfoService;

    @Autowired
    private ArticleTableInfoTask articleTableInfoTask;

    /**
     * 保存用户文章信息采集表单
     * @param userId 用户 ID
     * @param bArticleTableInfo 请求实体类
     * @return 操作信息
     */
    @PostMapping(value = "/saveArticleTableInfo", produces = "application/json;charset=UTF-8")
    public RespData saveArticleTableInfo(@RequestHeader("userId") String userId, @RequestHeader("identifying") String identifying, @RequestBody BArticleTableInfo bArticleTableInfo) {
        if (Objects.isNull(bArticleTableInfo) || Objects.isNull(bArticleTableInfo.getArticleId())) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer finalArticleId = null;
        Integer currentArticleUserId = articleInfoService.getArticleUserInfo(bArticleTableInfo.getArticleId());

        // 如果根据当前文章 ID 获取到的用户 ID 不为空且等于该文章的所有者，则表示该文章属于用户文章
        if (Objects.nonNull(currentArticleUserId) && currentArticleUserId.equals(bArticleTableInfo.getArticleOwner())) {
            finalArticleId = bArticleTableInfo.getArticleId();
        } else {// 平台文章

            ArticleInfo articleInfo = new ArticleInfo();
            articleInfo.setUserId(bArticleTableInfo.getArticleOwner());
            articleInfo.setShareArticleSourceId(bArticleTableInfo.getArticleId());

            finalArticleId = articleInfoService.getUserPlatformArticleShareStatus(articleInfo);
        }

        BArticleTableInfo entity = new BArticleTableInfo();
        entity.setUserId(Integer.parseInt(userId));

        if (!Objects.isNull(bArticleTableInfo.getUserName())) {
            entity.setUserName(bArticleTableInfo.getUserName());
        }

        if (!Objects.isNull(bArticleTableInfo.getCompany())) {
            entity.setCompany(bArticleTableInfo.getCompany());
        }

        if (!Objects.isNull(bArticleTableInfo.getJob())) {
            entity.setJob(bArticleTableInfo.getJob());
        }

        if (!Objects.isNull(bArticleTableInfo.getTelephone())) {
            entity.setTelephone(bArticleTableInfo.getTelephone());
        }

        if (!Objects.isNull(bArticleTableInfo.getWxId())) {
            entity.setWxId(bArticleTableInfo.getWxId());
        }

        entity.setArticleId(finalArticleId);
        entity.setArticleOwner(bArticleTableInfo.getArticleOwner());
        entity.setCreateTime(DateUtil.getCurDate());

        articleTableInfoService.saveArticleTableInfo(entity);

        // 发送推送消息
        articleTableInfoTask.sendArticleTableMsg(Integer.parseInt(userId), bArticleTableInfo.getArticleOwner(), identifying);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 根据 ID 获取文章采集信息
     * @param id 主键 ID
     * @return 文章采集信息
     */
    @GetMapping(value = "/getArticleTableInfoById", produces = "application/json;charset=UTF-8")
    public RespData getArticleTableInfoById(@RequestParam Integer id) {
        if (Objects.isNull(id)) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        CombinedAritcleTableInfo articleTableInfo = articleTableInfoService.getArticleTableInfoById(id);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("articleTableInfo", articleTableInfo);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 获取用户表单反馈汇总信息
     * @param userId 用户 ID
     * @return 用户表单反馈汇总信息
     */
    @GetMapping(value = "/getArticleTableSummary", produces = "application/json;charset=UTF-8")
    public RespData getArticleTableSummary(@RequestHeader("userId") String userId) {
        Integer finalUserId = Integer.parseInt(userId);

        int total = articleTableInfoService.getArticleTableCountByOwner(finalUserId);
        int threeDaysCount = articleTableInfoService.getLatestThreeDaysCount(finalUserId);

        List<CombinedAritcleTableDetail> detailList = articleTableInfoService.getArticleTableDetail(finalUserId);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        resultMap.put("total", total);
        resultMap.put("threeDaysCount", threeDaysCount);
        resultMap.put("detailList", detailList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }
}
